package com.zdxlz.fcmp.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdxlz.fcmp.business.dto.KmsConfigDto;
import com.zdxlz.fcmp.business.entity.KmsConfigEntity;
import com.zdxlz.fcmp.business.mapper.KmsConfigMapper;
import com.zdxlz.fcmp.business.service.IKmsConfigService;
import com.zdxlz.fcmp.business.vo.KmsConfigVo;
import com.zdxlz.fcmp.common.core.exception.BizException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.zdxlz.fcmp.common.core.constant.enums.ResCode.KMS_NAME_DUPLICATED;
import static com.zdxlz.fcmp.common.core.constant.enums.ResCode.KMS_NAME_DUPLICATED_NO_UPDATE;

/**
 * <p>
 * 量子密码资源配置表 服务实现类
 * </p>
 *
 * @author: aman
 * @since 2025-02-08
 */
@Service
public class KmsConfigServiceImpl extends ServiceImpl<KmsConfigMapper, KmsConfigEntity> implements IKmsConfigService {

    /**
     * @description 分页查询量子密码资源配置列表信息
     * @author aman
     * @date 2025/2/11 17:05
     */
    @Override
    public IPage<KmsConfigVo> getList(Page<KmsConfigEntity> page, KmsConfigDto kmsConfigDto) {
        LambdaQueryWrapper<KmsConfigEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(kmsConfigDto)) {
            queryWrapper
                    .eq(StrUtil.isNotBlank(kmsConfigDto.getName()), KmsConfigEntity::getName, kmsConfigDto.getName())
                    .eq(StrUtil.isNotBlank(kmsConfigDto.getAreaCode()), KmsConfigEntity::getAreaCode, kmsConfigDto.getAreaCode())
                    .like(StrUtil.isNotBlank(kmsConfigDto.getKmsAddress()), KmsConfigEntity::getKmsAddress, kmsConfigDto.getKmsAddress())
                    .like(StrUtil.isNotBlank(kmsConfigDto.getDeviceLocal()), KmsConfigEntity::getDeviceLocal, kmsConfigDto.getDeviceLocal());
        }
        Page<KmsConfigEntity> kmsConfigPageResult = baseMapper.selectPage(page, queryWrapper);

        List<KmsConfigVo> kmsConfigVoList = kmsConfigPageResult.getRecords().stream().map(this::convertToKmsConfigVo).collect(Collectors.toList());
        IPage<KmsConfigVo> kmsConfigVoPage = new Page<>(kmsConfigPageResult.getCurrent(), kmsConfigPageResult.getSize(), kmsConfigPageResult.getTotal());
        kmsConfigVoPage.setRecords(kmsConfigVoList);
        return kmsConfigVoPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createKmsConfig(KmsConfigEntity kmsConfigEntity) {
        Map<String, Object> oneEntity = Map.of("name", kmsConfigEntity.getName());
        List<KmsConfigEntity> ipsecVpnTemplateConfigEntities = baseMapper.selectByMap(oneEntity);
        if (CollectionUtil.isNotEmpty(ipsecVpnTemplateConfigEntities))
            throw new BizException(KMS_NAME_DUPLICATED.getCode(), KMS_NAME_DUPLICATED.getReason());
        baseMapper.insert(kmsConfigEntity);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateKmsConfig(KmsConfigEntity kmsConfigEntity) {
        Map<String, Object> oneEntity = Map.of("name", kmsConfigEntity.getName());
        List<KmsConfigEntity> ipsecVpnTemplateConfigEntities = baseMapper.selectByMap(oneEntity);
        if (CollectionUtil.isNotEmpty(ipsecVpnTemplateConfigEntities)) {
            if (!ipsecVpnTemplateConfigEntities.get(0).getId().equals(kmsConfigEntity.getId())) {
                throw new BizException(KMS_NAME_DUPLICATED_NO_UPDATE.getCode(), KMS_NAME_DUPLICATED_NO_UPDATE.getReason());
            }
        }
        return true;
    }

    /**
     * @description 量子密码资源配置实体转换为量子密码资源配置响应对象
     * @author aman
     * @date 2025/2/11 17:04
     */
    private KmsConfigVo convertToKmsConfigVo(KmsConfigEntity kmsConfigEntity) {
        KmsConfigVo kmsConfigVo = new KmsConfigVo();
        BeanUtil.copyProperties(kmsConfigEntity, kmsConfigVo);
        return kmsConfigVo;
    }
}
